# _*_ coding:UTF-8 _*_
from  DataCleaning import DataCleaning
from  SameConsumer import SameConsumer
import sys
import os
import time
from  concurrent.futures import ThreadPoolExecutor

def batchCalculator(trackDataPath):

    dc = DataCleaning(trackDataPath)
    allTrack, shopId, videoTime = dc.getDataPath(trackDataPath)

    currentPath = os.path.split(os.path.realpath(__file__))[0]  # 获取代码所在的当前路径
    configPath = os.path.join(os.path.join(currentPath, "config", shopId), "trackAnalyzeConfig.cfg")  # 配置文件的路径
    dbPara, cameraInDoor = dc.getConfigInfo(configPath)  #数据库参数,门口摄像头ID

    xml = os.path.join(currentPath, "config") + os.sep
    xml = os.path.join(xml, shopId) + os.sep  # 补全xml的路径
    doorLocation = dc.getCarLocation(xml)  # 车辆位置
    doorArea, doorName = dc.getCarCoord(doorLocation)

    startTimeshape = {}  # 进店时间戳
    allData = {}  # 所有摄像头下的轨迹数据（dataFrame）
    # 用多线程来同时运行多个摄像头下的轨迹分析程序
    task_result = {}

    with ThreadPoolExecutor(8) as ex:  # 多线程执行第三步
        task = {}
        for camerakey in allTrack:
            if "Channel" not in camerakey:
                continue
            task[camerakey] = ex.submit(dc.consumerTrack, allTrack[camerakey])

        keys = task.keys()
        for t in keys:
            try:
                result = task[t].result()
                if result is not None:
                    task_result[t] = result
            except Exception as e:
                print e

    keys = task_result.keys()
    for k in keys:
        tempRealId, tempIdMap, start, startTime, sale, data = task_result[k]
        if k == cameraInDoor:
            startTimeshape = startTime
        allData[k] = data

    sc = SameConsumer()
    allConsumer,allBatch = sc.findAllConsumerInDoor1(startTimeshape, doorArea, doorName, allData[cameraInDoor], dc, float(10), 30, 4000.0, 10)
    saleServiceNum = 0#sc.saleServiceInDoor1(startTimeshape,doorArea[doorName[0]].split(","),allData[cameraInDoor],dc,30,3000.0) #销售迎宾数
    
    print "allConsumer：" + str(allConsumer)
    print "batchNum：" + str(allBatch)
    print "saleServiceNum：" + str(saleServiceNum)

    return allBatch, saleServiceNum
